﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using EnvDTE;
using System.Reflection;
using System.Diagnostics;
using Microsoft.VisualStudio.VCProjectEngine;

namespace 微软中国.VsHelper
{
    public class VsHelper
    {
        private void WritePropertyFile(string path)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            Stream s = asm.GetManifestResourceStream("微软中国.VsHelper.Resources.VsHelper.props");
            if (null == s)
                return;

            byte[] bytes = new byte[s.Length];
            if (s.Length > int.MaxValue)
            {
                s.Write(bytes, 0, int.MaxValue);
                s.Write(bytes, int.MaxValue, (int)(s.Length - int.MaxValue));
            }
            else
            {
                s.Read(bytes, 0, (int)s.Length);
            }
            Directory.CreateDirectory(path);
            File.WriteAllBytes(path + "\\VsHelper.props", bytes);
            s.Close();
        }
        public void ConfiguringVCProject(VCProject vc_prj)
        {
            foreach (VCConfiguration vc_cfg in vc_prj.Configurations as IVCCollection)
            {
                ConfiguringVCConfiguration(vc_cfg);
            }
        }
        private void ConfiguringVCConfiguration(VCConfiguration vc_cfg)
        {
            AddVCPropertySheet(vc_cfg);

        }
        private void AddVCPropertySheet(VCConfiguration vc_cfg)
        {
            VCProject vc_prj = vc_cfg.project as VCProject;

            string path = "\\detail" + "\\" + "\\" + vc_cfg.ConfigurationName;
            WritePropertyFile(vc_prj.ProjectDirectory + path);
            string prop_name = "detail\\" + vc_cfg.ConfigurationName + "\\VsHelper.props";
            vc_cfg.AddPropertySheet(prop_name);
            IVCCollection props = vc_cfg.PropertySheets as IVCCollection;
            VCPropertySheet vc_prop = props.Item("VsHelper") as VCPropertySheet;
            if (null != vc_prop)
                ConfiguringVCPropertySheet(vc_prop);
        }
        private void ConfiguringVCPropertySheet(VCPropertySheet vc_prop)
        {
            SetUserMacro(vc_prop, "LIB_DIR", "lib\\");
            SetUserMacro(vc_prop, "BIN_DIR", "bin\\");
            SetUserMacro(vc_prop, "PDB_DIR", "pdb\\");
            SetUserMacro(vc_prop, "BUILD_DIR", "build\\");
            SetUserMacro(vc_prop, "PRODUCT_NAME", null);

            SetUserMacro(vc_prop, "RT_OPT", null);              // -sd -s -d or null
            SetUserMacro(vc_prop, "LINK_OPT", null);            // static or null
            SetUserMacro(vc_prop, "THREAD_OPT", null);          // -mt or null

            SetUserMacro(vc_prop, "VC_TOOLSET", "vc90");
            vc_prop.Save();
        }
        private void SetUserMacro(VCPropertySheet prop, string name, string value)
        {
            if (null != prop)
            {
                foreach (VCUserMacro macro in prop.UserMacros)
                {
                    if (macro.Name == name)
                    {
                        macro.Value = value;
                        return;
                    }
                }
                prop.AddUserMacro(name, value);
            }
        }
    }
}
